//
//  HNSH_GetAuthCodeAlert.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/27.
//

#import "HNSH_GetAuthCodeAlert.h"
#import "HNSH_PopupController.h"

#import "ST_TimeDownToors.h"

@interface HNSH_GetAuthCodeAlert ()

/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *nyPopView;

/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;
/* okButton */
@property (nonatomic, strong) UIButton *okButton;
/* cancelButton */
@property (nonatomic, strong) UIButton *cancelButton;

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/* codeField */
@property (nonatomic, strong) UITextField *codeField;
/* codeButton */
@property (nonatomic, strong) UIButton *codeButton;

@end

@implementation HNSH_GetAuthCodeAlert

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)showView {
    self.nyPopView = [[HNSH_PopupController alloc]init];
    self.nyPopView.dismissOnMaskTouched = YES;
    self.nyPopView.layoutType = PopupLayoutTypeCenter;
    self.nyPopView.slideStyle = PopupSlideStyleFade;
    [self.nyPopView presentContentView:self duration:0.25 springAnimated:YES inView:kSTWindow];
    self.nyPopView.popupView.layer.cornerRadius = 16.0f;
    self.nyPopView.popupView.layer.masksToBounds = YES;
}

- (void)closeView {
    [self.nyPopView dismissWithDuration:0.25 springAnimated:YES];
}

- (void)configUI {
    [self addSubview:self.titleLabel];
    [self addSubview:self.desLabel];
    [self addSubview:self.cancelButton];
    [self addSubview:self.okButton];
    
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.codeField];
    [self.bgView addSubview:self.codeButton];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(22);
    }];
    
    [self.desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(118);
        make.right.mas_equalTo(-16);
        make.left.mas_equalTo(16);
    }];
    
}

#pragma mark lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.text = @"短信验证";
    }
    return _titleLabel;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _desLabel.font = [UIFont systemFontOfSize:12];
        _desLabel.numberOfLines = 0;
        _desLabel.hidden = YES;
        _desLabel.text = [NSString stringWithFormat:@"验证码已发送至%@",[NSString transPhoneWithMobile:[ST_UserManager shareInstance].user.phone]];
    }
    return _desLabel;
}

- (UIButton *)okButton {
    if (!_okButton) {
        _okButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _okButton.frame = CGRectMake(32+(self.width-48)/2, 159, (self.width-48)/2, 40);
        [_okButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        [_okButton setTitle:@"确定" forState:UIControlStateNormal];
        _okButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _okButton.backgroundColor = [UIColor appGreenColor];
        _okButton.layer.cornerRadius = 20.0f;
        _okButton.layer.masksToBounds = YES;
        WeakSelf(weakSelf);
        [[_okButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (![weakSelf.codeField.text isNotBlank]) {
                [kSTWindow makeToast:@"请输入验证码"];
                return;
            }
            [HNSH_UserNetServer requestUserCancellationWithForce:1 sms_code:weakSelf.codeField.text success:^(ST_BaseNetModel * _Nullable responseObject) {
                if(responseObject.code == 0) {
                    [kSTWindow makeToast:@"注销成功"];
                    [weakSelf closeView];
                    [ST_UserManager logOut];
                }
            } Failure:^(NSError * _Nullable error) {
                
            }];
        }];
    }
    return _okButton;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.frame = CGRectMake(16, 159, (self.width-48)/2, 40);
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _cancelButton.backgroundColor = [UIColor appBGColor];
        _cancelButton.layer.cornerRadius = 20.0f;
        _cancelButton.layer.masksToBounds = YES;
        WeakSelf(weakSelf);
        [[_cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
        }];
    }
    return _cancelButton;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(16, 66, self.width-32, 44)];
        _bgView.backgroundColor = [UIColor appBGColor];
        _bgView.layer.cornerRadius = 8.0f;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UITextField *)codeField {
    if (!_codeField) {
        _codeField = [[UITextField alloc]initWithFrame:CGRectMake(12, 0, 120, 44)];
        _codeField.textColor = [UIColor appBlackLabelColor];
        _codeField.font = [UIFont boldSystemFontOfSize:14];
        NSMutableDictionary * attDic = [@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#D3D3D3"], NSFontAttributeName:_codeField.font} mutableCopy];
        NSMutableAttributedString * attPlace = [[NSMutableAttributedString alloc] initWithString:@"请输入验证码" attributes:attDic];
        _codeField.attributedPlaceholder = attPlace;
        _codeField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _codeField;
}

- (UIButton *)codeButton {
    if (!_codeButton) {
        _codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _codeButton.frame = CGRectMake(self.bgView.width-94, 0, 94, 44);
        [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_codeButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _codeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        WeakSelf(weakSelf);
        [[_codeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *sendButton = x;
            [HNSH_UserNetServer requestSendCodeWithUserName:[ST_UserManager shareInstance].user.phone    type:30 success:^(ST_BaseNetModel * _Nullable responseObject) {
                if (responseObject.code == 0) {
                    weakSelf.desLabel.hidden = NO;
                    [[ST_TimeDownToors shareTimer] startTimerWithTime:60 complate:^(NSInteger time) {
                        NSString *timeStr = [NSString stringWithFormat:@"%lds",time];
                        [sendButton setTitle:timeStr forState:UIControlStateNormal];
                        sendButton.enabled = NO;
                        if (time == 0) {
                            [sendButton setTitle:@"重新发送" forState:UIControlStateNormal];
                            sendButton.enabled = YES;
                            weakSelf.desLabel.hidden = YES;
                        }
                    }];
                }
            } Failure:^(NSError * _Nullable error) {

            }];
        }];
    }
    return _codeButton;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
